package web;

import beans.LoginStatus;
import beans.User;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;
import utils.ResultUtils;

import java.io.IOException;

/**
 * 专门向前端响应用户信息
 */
@WebServlet("/user/getUser")
public class GetUserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.从session中获取用户数据
        User existUser = (User)request.getSession().getAttribute("existUser");

        //2.根据existUser来向页面响应不同的数据
        String responseData = null;
        if (existUser != null) {
            //说明用户已登录
            responseData = ResultUtils.result(LoginStatus.LOGIN_SUCCESS, existUser);
        } else {
            //说明用户未登录 或 用户已登出
            responseData = ResultUtils.result(LoginStatus.LOGGED_OUT, null);
        }
        response.getWriter().write(responseData);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
